package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Product;
import com.zretc.entity.Type;
import com.zretc.service.ProductService;
import com.zretc.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/product")
public class ProductController {

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @RequestMapping("/getByPageProduct")
    @ResponseBody
    public PageInfo<Product> getByPageProduct(@RequestParam(value = "pageNum") Integer pageNum,
                                              @RequestParam(value = "pageSize") Integer pageSize,
                                              @RequestParam(value = "sellerAccount") String sellerAccount,
                                              @RequestParam String keyword,
                                              String orderBy,
                                              Boolean desc) {


        logger.info("sellerAccount=========" + sellerAccount);
        logger.info("pageNum=========" + pageNum);
        logger.info("pageSize=========" + pageSize);
        logger.info("keyword=========" + keyword);

        Map<String, Object> map = new HashMap<>();
        map.put("sellerAccount", sellerAccount);

        if (!"".equals(keyword) && keyword != null)
            map.put("keyword", "%" + keyword + "%");
        if (pageNum != null)
            map.put("pageNum", pageNum);
        if (pageSize != null)
            map.put("pageSize", pageSize);

        if (orderBy != null && !"".equals(orderBy)) {
            map.put("orderBy", orderBy);
            if (desc)
                map.put("desc", true);
        }
        logger.info("========" + map);
        //logger.info("=========" + productService.getBySellerAccount(map));
        return productService.getBySellerAccount(map);

    }

    @RequestMapping("/add")
    @ResponseBody
    public boolean add(@RequestParam(value = "typeId") Integer typeId,
                       @RequestParam(value = "sellerId") Integer sellerId,
                       @RequestParam(value = "productName") String productName,
                       @RequestParam(value = "productInventory") Integer productInventory,
                       @RequestParam(value = "productPrice") Integer productPrice,
                       @RequestParam(value = "productStatus") Integer productStatus,
                       @RequestParam(value = "productIntroduce") String productIntroduce,
                       MultipartFile logo) {

        String productPicture = Util.getProductLogo(logo);
        Product product = new Product(productName, productInventory,
                productPrice, productPicture, Util.getNowTime(),
                productStatus, productIntroduce, sellerId,
                null, typeId, null);
        logger.debug(product.toString());
        int op = productService.add(product);
        if (op == 0) {
            return false;
        }
        return true;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public boolean delete(Integer productId) {
        int sc = productService.delete(productId);
        if (sc == 0) {
            return false;
        }
        return true;
    }

    public boolean update(@RequestParam(value = "productId") Integer productId,
                          Integer typeId,
                          String productName,
                          Integer productInventory,
                          Integer productPrice,
                          String productPicture,
                          String productTime,
                          Integer productStatus,
                          String productIntroduce) {
        Map<String, Object> param = new HashMap<>();
        param.put("typeId", typeId);
        param.put("productId", productId);
        param.put("productName", productName);
        param.put("productInventory", productInventory);
        param.put("productPrice", productPrice);
        param.put("productPicture", productPicture);
        param.put("productTime", productTime);
        param.put("productStatus", productStatus);
        param.put("productIntroduce", productIntroduce);
        int xiu = productService.update(param);
        if (xiu == 0) {
            return false;
        }
        return true;
    }

    @RequestMapping("/allByPage")
    @ResponseBody
    public PageInfo<Product> findProductAll(@RequestParam("pageNum") int pageNum,
                                            @RequestParam("pageSize") int pageSize) {
        PageInfo<Product> productAll = productService.findProductAll(pageNum, pageSize);

        return productAll;

    }

    @RequestMapping("/likeName")
    @ResponseBody
    public PageInfo<Product> findProductLikeName(String productName,
                                                 @RequestParam("pageNum") int pageNum,
                                                 @RequestParam("pageSize") int pageSize) {
        PageInfo<Product> productAll = productService.findProductLikeName(productName, pageNum, pageSize);

        return productAll;

    }

}
